package com.dreamlike.demo.HttpUtil.Core.Validator.impl;

import com.dreamlike.demo.HttpUtil.Core.HttpDefinition;
import com.dreamlike.demo.HttpUtil.Core.Validator.HttpDefinitionValidator;
import com.dreamlike.demo.HttpUtil.Core.Validator.HttpDefinitionValidateResult;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

public class AsyncValidator implements HttpDefinitionValidator {
    @Override
    public boolean validate(HttpDefinition httpDefinition, HttpDefinitionValidateResult h) {

        if (!httpDefinition.isAsync()) {
            return true;
        }
        Class<?> type = httpDefinition.getMethod().getReturnType();
        if (Future.class.isAssignableFrom(type) && CompletableFuture.class.equals(type)) {
            return true;
        }
        h.addInfo(httpDefinition.getOriginalMethodDesc()+"返回值必须为Future类型或者CompletableFuture类型", this);
        return false;
    }
}
